package com.eco.fanliapp.admin.controller.web;


import com.eco.fanliapp.admin.annotation.SysLogAnnotation;
import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.MessagePush;
import com.eco.fanliapp.service.IMessagePushService;
import com.eco.fanliapp.utils.EmojiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
public class MessagePushController extends BaseController {


    @Autowired
    IMessagePushService pushService;

    @SysLogAnnotation(value = "消息推送", userType = 2)
    @RequestMapping(method = RequestMethod.POST, value = "/web/message/push/add")
    public ServerResponse add(@RequestBody MessagePush messagePush) {
        Integer timeType = messagePush.getTimeType();
        if (timeType == 2) {
            //定时推送
            if (messagePush.getPushTime() == null) {
                return ServerResponse.createByErrorCodeMessage(201, "时间不能为空");
            } else {
                if (messagePush.getPushTime().compareTo(new Date()) == -1) {
                    return ServerResponse.createByErrorCodeMessage(201, "推送时间小于当前时间");
                }
            }
        }
        if (getAuthentication().getRoleId() != 0) {
            messagePush.setCompanyId(getAuthentication().getCompanyId());
        }
        messagePush.setAdminId(getAuthentication().getUserId());
        messagePush.setTitle(EmojiUtil.resolveToByteFromEmoji(messagePush.getTitle()));
        messagePush.setContent(EmojiUtil.resolveToByteFromEmoji(messagePush.getContent()));
        return pushService.insert(messagePush, timeType);
    }

}
